<?php



require_once 'calendarSyncCommon.php';


$client = new apiClient();
$client->setApplicationName("Google Calendar Sync");

$calender_id="";
$cal = new apiCalendarService($client);

/*
if (isset($_GET['logout'])) {
  unset($_SESSION['google_token']);
}
*/
        
if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['google_token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['google_token'])) {
  $client->setAccessToken($_SESSION['google_token']);
}

if ($client->getAccessToken()) {
  $calendar = $cal->calendars->get("primary");// Get Primary Calender 
  $calender_id = $calendar->getId();//Set ID of primary Calender
  
  $googleevent = new GoogleEvent();
  $eventslist = $googleevent->getEventList($client,$calender_id);
  //$eventslist = $googleevent->getEventList($client, "mubashir@cloudforce-consulting.com");
//  $events = $cal->events;
//  $eventslist = $events->listEvents("4bn78ptkeihiqu363fmvdg8fsc@group.calendar.google.com");
  print "<h1>Calender Events</h1><pre>" . print_r($eventslist,true) . "<pre>";

$_SESSION['google_token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

?>
